package unit;

import junit.framework.TestCase;

import com.google.code.netcheckin.aprs.AprsFormatter;
import com.google.code.netcheckin.io.Ascii;

public class UAprsFormatter extends TestCase {

	public static final long ASTAMP = 699014000;      // Jan 9, 1970 2:10:14 AM
	public static final  String CORRECT_STAMP = "090210z";// 09      02 10

	protected AprsFormatter formatter;

	public UAprsFormatter(String name) {
		super(name);
	}

	public void setUp() {
		formatter = new AprsFormatter();
	}

	public void tearDown() {
		formatter = null;
	}

	public void testAAAprsTimestamp() {
		String answer = formatter.formatAprsTimestamp(ASTAMP);
		assertEquals("Wrongly formatted time", CORRECT_STAMP, answer);
	}

	public void testABFormatDoubleZero() {
		String answer = formatter.formatDoubleZero(0);
		assertEquals("not 00", "00", answer);
		answer = formatter.formatDoubleZero(5);
		assertEquals("not 05", "05", answer);
	}

	public void testACFormatTripleZero() {
		String answer = formatter.formatTripleZero(0);
		assertEquals("not 000", "000", answer);
		answer = formatter.formatTripleZero(5);
		assertEquals("not 005", "005", answer);
	}

	public void testADFormatCoordinateMinutes() {
		double minutes = 0.01;	// test zero fill
		String fmted = formatter.formatCoordinateMinutes(minutes);
		assertEquals("1 wrong", "00.01", fmted);

		minutes = 0.017;	// test no rounding
		fmted = formatter.formatCoordinateMinutes(minutes);
		assertEquals("2 wrong", "00.01", fmted);

		minutes = 32.17;	// test plain
		fmted = formatter.formatCoordinateMinutes(minutes);
		assertEquals("3 wrong", "32.17", fmted);
	}

	public void testAGParseLongitudeDegrees() {
	   try {
		int intv = formatter.parseCoordinateLongitudeDegrees("49");
		assertEquals("49 wrong", 49, intv);
		intv = formatter.parseCoordinateLongitudeDegrees("119");
		assertEquals("119 wrong", 119, intv);
		intv = formatter.parseCoordinateLongitudeDegrees("9");
		assertEquals("9 wrong", 9, intv);
		
	   } catch (Exception e) {
		System.out.println(e.getMessage());
		e.printStackTrace();
		fail(e.getMessage());
		
	   }

	   try {
		int intv = formatter.parseCoordinateLongitudeDegrees("31x");
		fail("31x should fail");
	   } catch (Exception e) {
		assertNotNull("OK", e.getMessage());
	   }

	   try {
		int intv = formatter.parseCoordinateLongitudeDegrees("4.7");
		assertEquals("4.7", 4, intv);
		fail("4.7 should fail");
	   } catch (Exception e) {
		assertNotNull("OK", e.getMessage());
	   }

	   try {
		int intv = formatter.parseCoordinateLongitudeDegrees("181");
		fail("181 should fail");
	   } catch (Exception e) {
		assertNotNull("OK", e.getMessage());
	   }

	   try {
		int intv = formatter.parseCoordinateLongitudeDegrees("-81");
		fail("-81 should fail");
	   } catch (Exception e) {
		assertNotNull("OK", e.getMessage());
	   }

	}

	public void testAHParseCoordinateLatitude() {
	   try {
		int intv = formatter.parseCoordinateLatitudeDegrees("91");
		fail("91 should fail");
	   } catch (Exception e) {
		assertNotNull("OK", e.getMessage());
	   }
	   try {
		int intv = formatter.parseCoordinateLatitudeDegrees("-81");
		fail("-81 should fail");
	   } catch (Exception e) {
		assertNotNull("OK", e.getMessage());
	   }
	}

	public void testAIParseMinutes() {
	   try {
		double doubv = formatter.parseCoordinateMinutes("49.29");
		assertEquals("49 wrong", 49.29, doubv);
		doubv = formatter.parseCoordinateMinutes("1.9");
		assertEquals("1.9 wrong", 1.9, doubv);
		doubv = formatter.parseCoordinateMinutes("9");
		assertEquals("9 wrong", 9.0, doubv);
	   } catch (Exception e) {
		System.out.println(e.getMessage());
		e.printStackTrace();
		fail(e.getMessage());
		
	   }
	   try {
		double doubv = formatter.parseCoordinateMinutes("x9.7");
		fail("x9.7 did not throw exception");
	   } catch(Exception e) {
		assertNotNull("x9.7 should throw exc", e);
	   }
	   try {
		double doubv = formatter.parseCoordinateMinutes("8x");
		fail("8x should throw exception");
	   } catch(Exception e) {
		assertNotNull("should throw exc", e);
	   }
	   try {
		double doubv = formatter.parseCoordinateMinutes("90.01");
		fail("90.01 should throw exception");
	   } catch(Exception e) {
		assertNotNull("should throw exc", e);
	   }
	   try {
		double doubv = formatter.parseCoordinateMinutes("-0.01");
		fail("-0.01 should throw exception");
	   } catch(Exception e) {
		assertNotNull("should throw exc", e);
	   }
	}

	public void testAJTestIsValidIconSymbol() {
		int idx = 0;
		assertTrue("first failed", AprsFormatter
			.isValidIconSymbol(AprsFormatter.VALID_ICONS[idx]));
		idx = AprsFormatter.VALID_ICONS.length - 1;
		assertTrue("second failed", AprsFormatter
			.isValidIconSymbol(AprsFormatter.VALID_ICONS[idx]));
		assertFalse("non-sym passed", AprsFormatter
			.isValidIconSymbol(Ascii.V_TILDE));
	}

	public void testAKTestIsIconOverlayable() {
		int idx = 0;
		assertTrue("first failed", AprsFormatter
			.isIconSymbolOverlayable(AprsFormatter.OVERLAY_ICONS[idx]));
		idx = AprsFormatter.OVERLAY_ICONS.length - 1;
		assertTrue("second failed", AprsFormatter
			.isIconSymbolOverlayable(AprsFormatter.OVERLAY_ICONS[idx]));
		assertFalse("non-sym passed", AprsFormatter
			.isIconSymbolOverlayable(Ascii.V_b));
	}

	public void testALIsValidIconBytes() {
		assertTrue("first failed", AprsFormatter.isValidIcon(
			Ascii.V_SLASH, Ascii.V_MINUS));
		assertTrue("second failed", AprsFormatter.isValidIcon(
			Ascii.V_BACK_SLASH, Ascii.V_BRACELEFT));
		assertTrue("thirs failed", AprsFormatter.isValidIcon(
			Ascii.V_M, Ascii.V_AMPERSAND));
		assertFalse("fourth failed", AprsFormatter.isValidIcon(
			Ascii.V_N, Ascii.V_w));
	}

	public void testAMIsValidIconString() {
		assertTrue("first failed", AprsFormatter.isValidIcon("/t"));
		assertFalse("second failed", AprsFormatter.isValidIcon("x/"));
	}

}
